package de.bazinga.rotw.menu;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.BasicSyncManager;
import de.bazinga.rotw.network.Client;
import de.bazinga.rotw.network.Server;

public class LoadingState extends Menu {

	public LoadingState() {
		super("loadingMenu");
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/loading.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	
	protected void initMenuItems() {
		
	}
	
	protected void handleClick(String item) {
		
	}
	
	public void update(float tpf) {
		super.update(tpf);
		
	}
	
	public void startServer(int currLevel) {
		// We need to create this here, because otherwise we would be in another thread and 
		// the Collision-detecting wouldn't work
        ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).recreatePhysicsSpace();
        
		// Reset Game only if server
		// clients will need to get map-info first
		((RotwGameState)GameStateManager.getInstance().getChild("gameState")).
				reset(currLevel);
		
		
		try{
			BasicSyncManager syncManager = 
				new Server(((RotwGameState)GameStateManager.getInstance().getChild("gameState")));
			
			((RotwGameState)GameStateManager.getInstance().getChild("gameState")).setSyncManager(syncManager);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		Game.stopMenuSound();
		GameStateManager.getInstance().getChild("gameState").setActive(true);
		this.setActive(false);
	}
	
	public void startClient(String ip) {
		// We need to create this here, because otherwise we would be in another thread and 
		// the Collision-detecting wouldn't work
        ((RotwGameState)GameStateManager.getInstance().getChild("gameState")).recreatePhysicsSpace();
        
		BasicSyncManager syncManager = null;
		try{
			syncManager = new Client(((RotwGameState)GameStateManager.getInstance().getChild("gameState")),ip);
			
			((RotwGameState)GameStateManager.getInstance().getChild("gameState")).setSyncManager(syncManager);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		if(((Client)syncManager).getClient().getServerConnection() != null) {
			GameStateManager.getInstance().getChild("gameState").setActive(true);
			Game.stopMenuSound();
		}
		this.setActive(false);
	}
}